import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class Album {
	
	

	private final static List<String> listAlbum = new ArrayList<String>();

	public static void addAlbum(String titre) {
		listAlbum.add(titre);
	}

	public static void removeAlbum() {
		listAlbum.removeAll(listAlbum);
	}

	public static List<String> getAlbums() {
		return listAlbum;
	}

	/**
	 *
	 */
	public static void getAlbums(int visible){
		
		String select = " SELECT titre FROM Album where visible = " + visible;
		try {
			Statement stmt = Application.getStmt();
			stmt.executeUpdate("SET TRANSACTION READ ONLY");
			ResultSet res = stmt.executeQuery(select);
			
			while (res.next())
			{
				Album.addAlbum(res.getString(1));
				System.out.println("toto" + res.getString(1));
			}
			stmt.executeUpdate("COMMIT");
			res.close();
			
		} catch (SQLException e) {
			System.out.println("Erreur");
			e.printStackTrace();
		}
		
	}
	
	public static String afficher(Object object, int ordre ){
		
		String select = " SELECT i.chemin FROM Album a , Image i  , AlbumContient c  where i.idimage = c.idimage and c.idAlbum = a.idAlbum and a.titre like '" + object + "' and c.numOrdre = " + ordre;
		
		String chemin = null ;
		try {
			Statement stmt = Application.getStmt();
			stmt.executeUpdate("SET TRANSACTION READ ONLY");
			ResultSet res = stmt.executeQuery(select);
			
			res.next();
			
		     chemin = res.getString(1);			
			
			stmt.executeUpdate("COMMIT");
			res.close();
			
			
		} catch (SQLException e) {
			System.out.println("Erreur");
			e.printStackTrace();
		}
		return chemin;
		
	}
	
	public static void ajouterPhoto(String titre,String chemin){
		
		
		int idImage = 2;
		
		int idAlbum = 1 ;
		
		int ordre = 0 ;
		
		/*String select = " SELECT Max(idImage) from Image ";
		
		try {
			Statement stmt = Application.getStmt();
			stmt.executeUpdate("SET TRANSACTION READ ONLY");
			ResultSet res = stmt.executeQuery(select);
			
			res.next();
			
		     idImage = res.getInt(1);			
			
			stmt.executeUpdate("COMMIT");
			res.close();
			
			
		} catch (SQLException e) {
			System.out.println("Erreur");
			e.printStackTrace();
		}
		
	    select = " SELECT idAlbum from Album where titre like " + titre;
		
		try {
			Statement stmt = Application.getStmt();
			stmt.executeUpdate("SET TRANSACTION READ ONLY");
			ResultSet res = stmt.executeQuery(select);
			
			res.next();
			
		     idAlbum = res.getInt(1);			
			
			stmt.executeUpdate("COMMIT");
			res.close();
			
			
		} catch (SQLException e) {
			System.out.println("Erreur");
			e.printStackTrace();
		}
		
		
		
		*
		*
		*/
		
	    String select = " SELECT max(NumOrdre) from AlbumContient where idAlbum = " + idAlbum;
		
		try {
			Statement stmt = Application.getStmt();
			stmt.executeUpdate("SET TRANSACTION READ ONLY");
			ResultSet res = stmt.executeQuery(select);
			
			res.next();
			
		     ordre = res.getInt(1);			
			
			stmt.executeUpdate("COMMIT");
			res.close();
			
			
		} catch (SQLException e) {
			System.out.println("Erreur");
			e.printStackTrace();
		}
		
		idImage = idImage + 1 ;
		ordre = ordre + 1 ;
	
		String insertImage = "insert into Image values('"+idImage+"','"+chemin+"', '' )" ;
		String insertContientAlbum = "insert into AlbumContient values('"+idAlbum+"','"+idImage+"','"+ordre+"','','' ) ";
		
		System.out.println(insertImage);
		System.out.println(insertContientAlbum);
		try {
			Statement stmt = Application.getStmt();
			stmt.executeUpdate("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
			stmt.executeQuery(insertImage);	
			stmt.executeQuery(insertContientAlbum);
			stmt.executeUpdate("COMMIT");
		
		} catch (SQLException e) {
			System.out.println("Erreur");
			e.printStackTrace();
		}
		
		
		
		
	}
	
	public static InfoImage GetInfoImage(String titre, int ordre)
	{
		
		String select = " SELECT c.titre , c.commentaire FROM Album a , AlbumContient c  where c.idAlbum = a.idAlbum and a.titre like '" + titre + "' and c.numOrdre = " + ordre;
		
		InfoImage image = null;
		
		try {
			Statement stmt = Application.getStmt();
			stmt.executeUpdate("SET TRANSACTION READ ONLY");
			ResultSet res = stmt.executeQuery(select);
			
			res.next();
			
		   image = new InfoImage(res.getString(1), res.getString(2));
			
			stmt.executeUpdate("COMMIT");
			res.close();
			
			
		} catch (SQLException e) {
			System.out.println("Erreur");
			e.printStackTrace();
		}
		
		return image;		
		
	}
	
	
	
	public static void gererAlbum(){
		
	}
	
}
